package com.tianpengtech.admin.permission;
import java.util.Map;

import com.jfinal.plugin.activerecord.Model;
import com.tianpengtech.common.exception.ValidateException;
import com.tianpengtech.admin.AbstractAdmin;
import com.tianpengtech.common.model.permission.Permission;
import com.tianpengtech.common.model.role.Role;
import com.tianpengtech.common.search.LikeCondition;
import com.tianpengtech.common.search.QueryConstructor;
import com.tianpengtech.common.validator.Validator;
import com.tianpengtech.common.validator.rules.numberic.NumbericMustBeGreaterThan;
import com.tianpengtech.common.validator.rules.string.StringLengthBetween;
import org.apache.commons.lang.StringUtils;

public class PermissionAdminServiceImpl extends AbstractAdmin{

	@Override
	public void beforeAdd(Model<?> model) throws ValidateException {

		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(model.getStr(Permission.NAME),"权限名称只能输入1-50个字符信息",1,50));
		validator.addRule(new StringLengthBetween(model.getStr(Permission.PERMISSION),"权限名称只能输入1-150个字符信息",1,150));
		validator.addRule(new NumbericMustBeGreaterThan(model.getInt(Permission.REF_ID),0,"请选择所属的功能模块"));
		validator.excute();
	}

	@Override
	public void beforeQueryById(Model<?> model, int id) {

	}

	@Override
	public void beforeUpdate(Model<?> model) throws ValidateException {
		Validator validator = new Validator();
		validator.addRule(new StringLengthBetween(model.getStr(Permission.NAME),"权限名称只能输入1-50个字符信息",1,50));
		validator.addRule(new StringLengthBetween(model.getStr(Permission.PERMISSION),"权限名称只能输入1-150个字符信息",1,150));
		validator.addRule(new NumbericMustBeGreaterThan(model.getInt(Permission.REF_ID),0,"请选择所属的功能模块"));
		validator.addRule(new NumbericMustBeGreaterThan(model.getLong(Permission.ID),0,"ID必须大于零"));
		validator.excute();
	}

	@Override
	public void beforeDelete(Model<?> model) {

	}

	@Override
	public void builderSQLExpected(Model< ? > model,
			QueryConstructor queryConstruct, Map< String, Object > params) {
		if(StringUtils.isNotEmpty(model.getStr(Permission.NAME))){
			queryConstruct.and(new LikeCondition(Permission.NAME,"%"+model.getStr(Permission.NAME)+"%"));
		}
	}

	@Override
	public String tableName() {
		return Permission.dao.tableName();
	}

}